package com.laizhenghua.share.user.controller;

import com.laizhenghua.share.common.core.context.SecurityContextHolder;
import com.laizhenghua.share.common.core.domain.R;
import com.laizhenghua.share.common.core.utils.poi.ExcelUtil;
import com.laizhenghua.share.common.core.web.controller.BaseController;
import com.laizhenghua.share.common.core.web.domain.AjaxResult;
import com.laizhenghua.share.common.core.web.page.TableDataInfo;
import com.laizhenghua.share.user.api.domain.entity.UserInfo;
import com.laizhenghua.share.user.api.domain.vo.UserVo;
import com.laizhenghua.share.user.service.IUserInfoService;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 用户Controller
 *
 * @author laizhenghua
 * @date 2025-10-10
 */
@Tag(name = "用户接口管理")
@RestController
@RequestMapping("/user-info")
public class UserInfoController extends BaseController {
    private final IUserInfoService iUserInfoService;

    @Autowired
    public UserInfoController(IUserInfoService iUserInfoService) {
        this.iUserInfoService = iUserInfoService;
    }

    @Operation(summary = "微信小程序授权登录")
    @GetMapping("/wx/login/{code}")
    public R<UserInfo> wxLogin(@PathVariable("code") String code) {
        return R.ok(iUserInfoService.wxLogin(code));
    }

    @GetMapping("/getLoginUserInfo")
    @Operation(summary = "获取用户信息")
    public R<UserVo> getLoginUserInfo() {
        Long userId = SecurityContextHolder.getUserId();
        UserInfo userInfo = iUserInfoService.getById(userId);
        UserVo userVo = new UserVo();
        userVo.setNickname(userInfo.getNickname());
        userVo.setWxOpenId(userInfo.getWxOpenId());
        userVo.setAvatar(userInfo.getAvatarUrl());
        userVo.setDepositStatus("0");
        return R.ok(userVo);
    }

    /**
     * 查询用户列表
     */
    @Operation(summary = "查询用户列表")
    @RequiresPermissions("user:userInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(UserInfo userInfo) {
        startPage();
        List<UserInfo> list = iUserInfoService.selectUserInfoList(userInfo);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @Operation(summary = "导出用户列表")
    @RequiresPermissions("user:userInfo:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserInfo userInfo) {
        List<UserInfo> list = iUserInfoService.selectUserInfoList(userInfo);
        ExcelUtil<UserInfo> util = new ExcelUtil<UserInfo>(UserInfo.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @Operation(summary = "获取用户详细信息")
    @RequiresPermissions("user:userInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(iUserInfoService.getById(id));
    }

    @Operation(summary = "获取用户详细信息")
    @GetMapping(value = "/get/{id}")
    public R<UserInfo> getUserInfo(@PathVariable("id") Long id) {
        return R.ok(iUserInfoService.getById(id));
    }

    /**
     * 新增用户
     */
    @Operation(summary = "新增用户")
    @RequiresPermissions("user:userInfo:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserInfo userInfo) {
        return toAjax(iUserInfoService.save(userInfo));
    }

    /**
     * 修改用户
     */
    @Operation(summary = "修改用户")
    @RequiresPermissions("user:userInfo:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserInfo userInfo) {
        return toAjax(iUserInfoService.updateById(userInfo));
    }

    /**
     * 删除用户
     */
    @Operation(summary = "删除用户")
    @RequiresPermissions("user:userInfo:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iUserInfoService.removeBatchByIds(Arrays.asList(ids)));
    }

    @GetMapping("/free/deposit")
    @Operation(summary = "是否免押金判断")
    public R<Boolean> isFreeDeposit() {
        return R.ok(iUserInfoService.isFreeDeposit());
    }
}
